//
//  TuiJianZuoPinVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/27.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "TuiJianZuoPinVC.h"
#import "tuiJIanZuoPinCell.h"

@interface TuiJianZuoPinVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) IBOutlet UICollectionView *collection_view;
@end

@implementation TuiJianZuoPinVC{
    RequestServices *service;
    NSInteger page_index;//页码
    NSMutableArray *list_arr;//列表数组
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"推荐作品";
    [self.collection_view registerNib:[UINib nibWithNibName:@"tuiJIanZuoPinCell" bundle:nil] forCellWithReuseIdentifier:@"tuiJIanZuoPinCell"];
    self.collection_view.delegate = self;
    self.collection_view.dataSource = self;
    
    list_arr = [[NSMutableArray alloc]init];
    service = [[RequestServices alloc]initWithDelegate:self];
    service.view = self.view;
    __weak __typeof(self)weakSelf = self;
    [self headerRefresh];
    self.collection_view.mj_header = [baseRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf headerRefresh];
    }];
    self.collection_view.mj_footer = [baseRefreshFooter footerWithRefreshingBlock:^{
        [weakSelf footerRefresh];
    }];
    self.collection_view.mj_footer.hidden = YES;
}

- (void)headerRefresh
{
    page_index = 1;
    [service tuiJianZuoPin:[NSString stringWithFormat:@"%ld",page_index] tag:8000];
}

- (void)footerRefresh
{
    page_index++;
    [service tuiJianZuoPin:[NSString stringWithFormat:@"%ld",page_index] tag:8001];
}


#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self.collection_view.mj_footer endRefreshing];
    [self.collection_view.mj_header endRefreshing];
    
    [self HideerrorView];
    [self HideNoDataView];
    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
    if (tag == 8000) {
        if ([tempArr CheckNSArrayData]) {
            list_arr = [TuiJianModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
            
        }
        if (tempArr.count == 0) {
            [self ShowNoDataView:self.collection_view Orgin:0];
        }
    }else {
        [list_arr addObjectsFromArray:[TuiJianModel mj_objectArrayWithKeyValuesArray:tempArr]];
    }
    
    [self.collection_view reloadData];
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self.collection_view.mj_footer endRefreshing];
    [self.collection_view.mj_header endRefreshing];
    [self ShowNewMsg:error.domain];
    if (tag == 8000) {
        [self ShowerrorView:self.collection_view Orgin:0];
    }
}


#pragma mark ----- collectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return list_arr.count;
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_W - 160)/2, SCREEN_H * 0.3);
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    tuiJIanZuoPinCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tuiJIanZuoPinCell" forIndexPath:indexPath];
    cell.model = list_arr[indexPath.item];
    return cell;
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
//行间距（同一行的cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
